# -*- encoding: utf-8 -*-
'''
Created on Apr 1, 2012

@author: LONG HOANG GIANG
'''
import sys
import os
import time
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
from CrawlerLib.http import Http
import workerpool
import simplejson as json
import re

template = '''<html>
<head>
    <meta content="text/html;charset=utf-8" http-equiv="content-type" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no" />
    <title>#TITLE#</title>
    <style type="text/css">
    body {
        text-align: justify;
        text-justify: newspaper;
    }
    </style>
</head>
<body>
    #CONTENT#
</body>
</html>
</html>
'''

class Task(workerpool.Job):
    
    def __init__(self, callback):
        self.callback = callback
    
    def run(self):
        self.callback
    
def getHtml(url, path):
    print 'start getHtml : {0}'.format(url)
    html = ''
    for i in range(1,3):
        html = Http.getHtml(url)
        if html != None and html != '' and html != 'false': break
        time.sleep(2)
    if html == '' or html == 'false': 
        print 'Khong Lay Duoc HTML'
        return
    html = json.loads(html)
    ohtml = re.sub("(#TITLE#)", TITLE, template)
    html = re.sub("(#CONTENT#)", html, ohtml)
    
#    html = template.format(html)
    f = open(path, 'w')
    f.write(html)
    f.close()

def process2(storyId, start, end, sp=0, prefix=''):
    
    if not os.path.exists(FOLDER_PATH): os.makedirs(os.path.dirname(FOLDER_PATH), 0777)
    pool = workerpool.WorkerPool(size=1)
    data = {'url': [], 'path': []}
    for page in range(start, end+1):
        url = 'http://www.wattpad.com/apiv2/storytext?id={0}&page={1}'.format(storyId, page)
        data['url'].append(url)
        path = "{0}{1}{2}.html".format(FOLDER_PATH, prefix, sp + page)
        data['path'].append(path)
        getHtml(url, path)
        
        
#    pool.map(getHtml, data['url'], data['path'])
#    pool.shutdown()
#    pool.wait()

if __name__ == '__main__':
    
    FOLDER_PATH = '/davinci/'
    TITLE = "Mật Mã Davinci"
    
#    process2('117542', 1, 141, prefix='a')
#    process2('117545', 1, 167, prefix='b')
#    process2('117546', 1, 212, prefix='c')
#    process2('117550', 302, 356, prefix='d')
#    process2('118448', 247, 265, prefix='e')
#    process2('118451', 1, 240, prefix='f')
#    process2('117980', 1, 276, prefix='g')
    process2('75881', 1, 123, prefix='')
    
    print '>> Finished'
    os._exit(1)